package com.aptech.validate;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.*;

public class AccountValidator {

    public static boolean validateUsername(String username) {
        Pattern pattern;
        Matcher matcher;

        final String USERNAME_PATTERN = "^[a-z0-9_-]{6,30}$";

        pattern = Pattern.compile(USERNAME_PATTERN);

        matcher = pattern.matcher(username);

        return matcher.matches();
    }

    public static boolean validatePassword(String password) {
        Pattern pattern;
        Matcher matcher;

        final String PASSWORD_PATTERN
                = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})";

        pattern = Pattern.compile(PASSWORD_PATTERN);

        matcher = pattern.matcher(password);

        return matcher.matches();
    }

    public static boolean validateEmail(String email) {
        Pattern pattern;
        Matcher matcher;

        final String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
                + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

        pattern = Pattern.compile(EMAIL_PATTERN);

        matcher = pattern.matcher(email);

        return matcher.matches();
    }

    public static boolean validateAddress(String address) {
        Pattern pattern;
        Matcher matcher;

        final String ADDRESS_PATTERN = "^[a-zA-Z0-9_ -]{3,50}$";

        pattern = Pattern.compile(ADDRESS_PATTERN);

        matcher = pattern.matcher(address);

        return matcher.matches();
    }

    public static boolean validateSupplierName(String name) {
        Pattern pattern;
        Matcher matcher;

        final String SUPPLIER_PATTERN = "^[a-zA-Z_ -.]{1,50}$";

        pattern = Pattern.compile(SUPPLIER_PATTERN);

        matcher = pattern.matcher(name);

        return matcher.matches();
    }

    public static boolean validateDateOfBirth(String dateOfBirth) {
        Pattern pattern;
        Matcher matcher;

        final String DATEOFBIRTH_PATTERN = "(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)";

        pattern = Pattern.compile(DATEOFBIRTH_PATTERN);

        matcher = pattern.matcher(dateOfBirth);

        return matcher.matches();
    }

//    public static boolean validateAge(String age) {
//        Pattern pattern;
//        Matcher matcher;
//
//        final String AGE_PATTERN = "^[0-9]{1,3}";
//
//        pattern = Pattern.compile(AGE_PATTERN);
//
//        matcher = pattern.matcher(age);
//
//        return matcher.matches();
//    }
    public static String EncryptPassword(String plainPassword) throws NoSuchAlgorithmException {
        // Encrypt password to md5
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.reset();

//        String password = String.valueOf(jpwChangePassword_NewPassword.getPassword());
        messageDigest.update(plainPassword.getBytes(),
                0, plainPassword.length());

        String md5 = new BigInteger(1, messageDigest.digest()).toString(16);

        return md5;
    }

    public static boolean validataDrugName(String drugname) {
        Pattern pattern;
        Matcher matcher;

        final String DRUGNAME_PATTERN = "^[a-zA-Z0-9_ -/()#]{1,100}$";

        pattern = Pattern.compile(DRUGNAME_PATTERN);

        matcher = pattern.matcher(drugname);

        return matcher.matches();
    }

}
